﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace CoVuaServer
{
    class DataProvider
    {
        SqlConnection sqlconn;
        public DataProvider(string svrName, string dbName, bool intergratedMode, string usrname, string pwd)
        {
            string connstr = string.Empty;
            if (intergratedMode == true)
            {
                connstr = "server=" + svrName + "; database=" + dbName + "; Integrated security = true";
            }
            else
            {
                connstr = "server=" + svrName + "; uid=" + usrname + "; pwd=" + pwd + "; database=" + dbName;
            }
            sqlconn = new SqlConnection(connstr);
        }

        public DataProvider(string connection)
        {
            //connstr = @"server = TOANTIEN\TOANTIEN;database= COVUA; Integrated security = true";
            sqlconn = new SqlConnection(connection);
        }
        public DataTable Execute(string strQuery)
        {
            SqlDataAdapter da = new SqlDataAdapter(strQuery, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }
        public void ExecuteNonQuery(string strquery)
        {
            SqlCommand sqlcom = new SqlCommand(strquery, sqlconn);
            sqlconn.Open();
            sqlcom.ExecuteNonQuery();
            sqlconn.Close();
        }
        public void Update(string strQuery, DataTable table)
        {
            SqlDataAdapter da = new SqlDataAdapter(strQuery, sqlconn);
            SqlCommandBuilder sqlcb = new SqlCommandBuilder(da);
            da.Update(table);
        }
    }
}
